package main

/*
#cgo CFLAGS: -I./HandleDecrypt/include
#cgo LDFLAGS: -L./HandleDecrypt/lib -lmy_ched -Wl,--rpath=./
#include "my_ched.h"
#include <stdlib.h>
*/
import "C"
import (
	"fmt"
	"os"
	"unsafe"
)

func main() {
	// 解密数据库用户名和密码 start
	if len(os.Args) < 3 {
		fmt.Println("请输入参数")
		os.Exit(1)
	}
	sourcePassword := os.Args[1]
	strKey := os.Args[2]
	var strPassword string
	var bytePassword []byte = make([]byte, len(sourcePassword)+130)
	var cLen C.int = C.int(len(sourcePassword))
	C.Decrypt(C.CString(strKey), C.CString(sourcePassword), cLen, (*C.uchar)(unsafe.Pointer(&bytePassword[0])), cLen+130)
	strPassword = byteString(bytePassword)
	fmt.Printf(strPassword)
}

func byteString(p []byte) string {
	for i := 0; i < len(p); i++ {
		if p[i] == 0 {
			return string(p[0:i])
		}
	}
	return string(p)
}

func substr(str string, start int, end int) string {
	rs := []rune(str)
	length := len(rs)
	if start < 0 || start > length {
		return ""
	}

	if end < 0 || end > length {
		return ""
	}

	if start >= end {
		return ""
	}

	return string(rs[start:end])
}
